<template>
  <button
    v-if="!tag"
    ref="buttonRef"
    :type="nativeType"
    :disabled="disabled || loading"
    :class="buttonClass"
    @click="handleClick"
    @mousedown="handleMousedown"
    @mouseup="handleMouseup"
    @mouseenter="handleMouseenter"
    @mouseleave="handleMouseleave"
    @focus="handleFocus"
    @blur="handleBlur"
  >
    <!-- 加载状态 -->
    <span v-if="loading" class="loading-icon">
      <el-icon><Loading /></el-icon>
    </span>
    
    <!-- 图标 -->
    <span v-if="icon" class="icon">
      <el-icon><component :is="icon" /></el-icon>
    </span>
    
    <!-- 文本 -->
    <span v-if="$slots.default || text" class="text">
      <slot v-if="$slots.default">{{ text }}</slot>
      <span v-else>{{ text }}</span>
    </span>
  </button>
  
  <!-- 自定义标签 -->
  <component
    v-else
    :is="tag"
    ref="buttonRef"
    :disabled="disabled || loading"
    :class="buttonClass"
    @click="handleClick"
    @mousedown="handleMousedown"
    @mouseup="handleMouseup"
    @mouseenter="handleMouseenter"
    @mouseleave="handleMouseleave"
    @focus="handleFocus"
    @blur="handleBlur"
  >
    <!-- 加载状态 -->
    <span v-if="loading" class="loading-icon">
      <el-icon><Loading /></el-icon>
    </span>
    
    <!-- 图标 -->
    <span v-if="icon" class="icon">
      <el-icon><component :is="icon" /></el-icon>
    </span>
    
    <!-- 文本 -->
    <span v-if="$slots.default || text" class="text">
      <slot v-if="$slots.default">{{ text }}</slot>
      <span v-else>{{ text }}</span>
    </span>
  </component>
</template>

<script setup lang="ts">
import { ref, computed, withDefaults, defineProps, defineEmits } from 'vue'
import type { Component } from 'vue'
import { ElIcon } from 'element-plus'
import { Loading } from '@element-plus/icons-vue'

// Props
interface Props {
  type?: 'primary' | 'success' | 'warning' | 'danger' | 'info' | 'default'
  text?: string
  size?: 'large' | 'default' | 'small' | 'mini'
  icon?: Component
  loading?: boolean
  disabled?: boolean
  plain?: boolean
  border?: boolean
  round?: boolean
  circle?: boolean
  loadingIcon?: Component
  href?: string
  target?: string
  tag?: string | Component
  nativeType?: 'button' | 'submit' | 'reset'
  autofocus?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  type: 'default',
  size: 'default',
  nativeType: 'button',
  disabled: false,
  loading: false,
  plain: false,
  border: true,
  round: false,
  circle: false,
  tag: '',
  target: '_self'
})

// Emits
const emit = defineEmits<{
  'click': [event: MouseEvent]
  'mousedown': [event: MouseEvent]
  'mouseup': [event: MouseEvent]
  'mouseenter': [event: MouseEvent]
  'mouseleave': [event: MouseEvent]
  'focus': [event: FocusEvent]
  'blur': [event: FocusEvent]
}>()

// Refs
const buttonRef = ref<HTMLButtonElement | HTMLElement | null>(null)

// 计算样式类名
const buttonClass = computed(() => {
  const classes: string[] = ['custom-button']
  
  // 类型
  classes.push(`button-${props.type}`)
  
  // 尺寸
  classes.push(`button-${props.size}`)
  
  // 状态
  if (props.plain) classes.push('button-plain')
  if (props.round) classes.push('button-round')
  if (props.circle) classes.push('button-circle')
  if (!props.border) classes.push('button-no-border')
  if (props.disabled || props.loading) classes.push('button-disabled')
  if (props.loading) classes.push('button-loading')
  
  return classes
})

// 事件处理
const handleClick = (event: MouseEvent) => {
  emit('click', event)
}

const handleMousedown = (event: MouseEvent) => {
  emit('mousedown', event)
}

const handleMouseup = (event: MouseEvent) => {
  emit('mouseup', event)
}

const handleMouseenter = (event: MouseEvent) => {
  emit('mouseenter', event)
}

const handleMouseleave = (event: MouseEvent) => {
  emit('mouseleave', event)
}

const handleFocus = (event: FocusEvent) => {
  emit('focus', event)
}

const handleBlur = (event: FocusEvent) => {
  emit('blur', event)
}

// 暴露方法
defineExpose({
  focus: () => buttonRef.value?.focus(),
  blur: () => buttonRef.value?.blur(),
  click: () => buttonRef.value?.click()
})
</script>

<style scoped>
.custom-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 500;
  line-height: 1.5;
  border-radius: 4px;
  transition: all 0.3s ease;
  cursor: pointer;
  outline: none;
  user-select: none;
  white-space: nowrap;
  border: 1px solid transparent;
}

/* 按钮类型 */
.button-primary {
  color: #fff;
  background-color: #409eff;
  border-color: #409eff;
}

.button-primary:hover:not(.button-disabled) {
  background-color: #66b1ff;
  border-color: #66b1ff;
}

.button-success {
  color: #fff;
  background-color: #67c23a;
  border-color: #67c23a;
}

.button-success:hover:not(.button-disabled) {
  background-color: #85ce61;
  border-color: #85ce61;
}

.button-warning {
  color: #fff;
  background-color: #e6a23c;
  border-color: #e6a23c;
}

.button-warning:hover:not(.button-disabled) {
  background-color: #ebb563;
  border-color: #ebb563;
}

.button-danger {
  color: #fff;
  background-color: #f56c6c;
  border-color: #f56c6c;
}

.button-danger:hover:not(.button-disabled) {
  background-color: #f78989;
  border-color: #f78989;
}

.button-info {
  color: #fff;
  background-color: #909399;
  border-color: #909399;
}

.button-info:hover:not(.button-disabled) {
  background-color: #a6a9ad;
  border-color: #a6a9ad;
}

.button-default {
  color: #303133;
  background-color: #fff;
  border-color: #dcdfe6;
}

.button-default:hover:not(.button-disabled) {
  color: #409eff;
  border-color: #c6e2ff;
  background-color: #ecf5ff;
}

/* 朴素按钮 */
.button-plain {
  background-color: #fff;
}

.button-plain.button-primary {
  color: #409eff;
}

.button-plain.button-primary:hover:not(.button-disabled) {
  background-color: #ecf5ff;
}

.button-plain.button-success {
  color: #67c23a;
}

.button-plain.button-success:hover:not(.button-disabled) {
  background-color: #f0f9eb;
}

.button-plain.button-warning {
  color: #e6a23c;
}

.button-plain.button-warning:hover:not(.button-disabled) {
  background-color: #fdf6ec;
}

.button-plain.button-danger {
  color: #f56c6c;
}

.button-plain.button-danger:hover:not(.button-disabled) {
  background-color: #fef0f0;
}

.button-plain.button-info {
  color: #909399;
}

.button-plain.button-info:hover:not(.button-disabled) {
  background-color: #f4f4f5;
}

/* 无边框按钮 */
.button-no-border {
  border: none;
}

/* 按钮尺寸 */
.button-large {
  padding: 12px 20px;
  font-size: 16px;
  border-radius: 6px;
}

.button-small {
  padding: 6px 12px;
  font-size: 13px;
  border-radius: 3px;
}

.button-mini {
  padding: 4px 8px;
  font-size: 12px;
  border-radius: 3px;
}

/* 圆角按钮 */
.button-round {
  border-radius: 20px;
}

/* 圆形按钮 */
.button-circle {
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: 50%;
}

.button-large.button-circle {
  width: 40px;
  height: 40px;
}

.button-small.button-circle {
  width: 32px;
  height: 32px;
}

.button-mini.button-circle {
  width: 28px;
  height: 28px;
}

/* 禁用状态 */
.button-disabled {
  opacity: 0.6;
  cursor: not-allowed;
  pointer-events: none;
}

/* 加载状态 */
.button-loading {
  position: relative;
}

/* 图标和文本 */
.icon,
.loading-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-right: 6px;
  font-size: inherit;
}

.text {
  display: inline-block;
}

/* 只有图标时的样式 */
.custom-button:not(.button-circle) .text:empty {
  display: none;
}

.custom-button .icon:only-child,
.custom-button .loading-icon:only-child {
  margin-right: 0;
}

/* 禁用文本选择 */
.custom-button {
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

/* 焦点样式 */
.custom-button:focus-visible {
  outline: 2px solid rgba(64, 158, 255, 0.5);
  outline-offset: 2px;
}

/* 活跃状态 */
.custom-button:active:not(.button-disabled) {
  transform: translateY(1px);
}

/* 链接样式 */
.custom-button[href] {
  text-decoration: none;
}

/* 响应式布局 */
@media (max-width: 768px) {
  .custom-button {
    padding: 6px 12px;
    font-size: 14px;
  }
  
  .button-large {
    padding: 10px 16px;
    font-size: 15px;
  }
  
  .button-small,
  .button-mini {
    padding: 4px 8px;
    font-size: 12px;
  }
}

@media (max-width: 480px) {
  .custom-button {
    font-size: 13px;
  }
  
  .button-large {
    font-size: 14px;
  }
}
</style>